// extract modules
var fs = require('fs');

// create server
var server = require('http').createServer();
var io = require('socket.io').listen(server);

// run server
server.listen(52273, function() {
	console.log('Server running at http://127.0.0.1:52273');
});

// add web server event handler
server.on('request', function(request, response) {
	// read html page
	fs.readFile('resources/room.html', function(error, data) {
		response.writeHead(200, {'Content-Type': 'text/html'});
		response.end(data);
	});
});

// add socket server event handler
io.sockets.on('connection', function(socket) {
	// join event
	socket.on('join', function(room) {
		socket.join(room);
		socket.set('room', room);
	});
	
	// message event
	socket.on('message', function(message) {
		socket.get('room', function(error, room) {
			io.sockets.in(room).emit('message', message);
		});
	});
});